package 文件IO.文件复制案例;

import java.io.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        //1.重要信息
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要复制的文件路径");
        String srcPath = sc.next();
        System.out.println("请输入要复制到的目标路径");
        String destPath = sc.next();

        //2。严重路径是否合法
        File srcFile = new File(srcPath);
        if(!srcFile.isFile()){
            System.out.println("该复制文件路径不合法");
            return;
        }
        File destFile = new File(destPath);
        if(!destFile.getParentFile().isDirectory()){
            System.out.println("输入的要复制的路径不合法");
            return;
        }

        //3.读写文件 可能是二进制
        try(InputStream inputStream = new FileInputStream(srcFile);
            OutputStream outputStream = new FileOutputStream(destFile)) {

            while(true){
                byte[] bytes = new byte[1024];
                int read = inputStream.read(bytes);
                if(read == -1){
                    System.out.println("复制完成");
                    break;
                }
                outputStream.write(bytes,0,read);//确保不满也可以写入
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
